package com.kaiwukj.android.ufamily.mvp.model;

import com.alibaba.fastjson.JSON;
import com.blankj.utilcode.util.GsonUtils;
import com.kaiwukj.android.mcas.integration.IRepositoryManager;
import com.kaiwukj.android.mcas.mvp.BaseModel;
import com.kaiwukj.android.ufamily.mvp.http.api.service.CommonService;
import com.kaiwukj.android.ufamily.mvp.http.api.service.MineService;
import com.kaiwukj.android.ufamily.mvp.http.api.service.OrderService;
import com.kaiwukj.android.ufamily.mvp.http.entity.base.ListResp;
import com.kaiwukj.android.ufamily.mvp.http.entity.params.OrderCreateParams;
import com.kaiwukj.android.ufamily.mvp.http.entity.params.OrderEvaluateParams;
import com.kaiwukj.android.ufamily.mvp.http.entity.params.OrderPayParams;
import com.kaiwukj.android.ufamily.mvp.http.entity.params.OrderRefundParams;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.HouseKeeperResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.MyAddressResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.OrderCreateResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.OrderDetailResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.OrderEvaluateResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.OrderResult;
import com.kaiwukj.android.ufamily.mvp.http.utils.RxUtils;
import com.kaiwukj.android.ufamily.mvp.ui.page.order.OrderContract;

import java.util.Map;

import javax.inject.Inject;

import io.reactivex.Observable;

public class OrderModel extends BaseModel implements OrderContract.Model {

    @Inject
    public OrderModel(IRepositoryManager repositoryManager) {
        super(repositoryManager);
    }


    @Override
    public Observable<ListResp<OrderEvaluateResult>> fetchEvaluate(int page, int pageSize, int type) {
        if (type == 1) {
            return mRepositoryManager.obtainRetrofitService(OrderService.class)
                    .fetchNotEvaluatedOrder(page, pageSize)
                    .compose(RxUtils.handleResult());
        }
        return mRepositoryManager.obtainRetrofitService(OrderService.class)
                .fetchEvaluatedOrder(page, pageSize)
                .compose(RxUtils.handleResult());
    }

    @Override
    public Observable<ListResp<OrderResult>> fetchOrder(int page, int pageSize, int status) {
        return mRepositoryManager.obtainRetrofitService(OrderService.class)
                .fetchOrder(page, pageSize, status)
                .compose(RxUtils.handleResult());
    }

    @Override
    public Observable<Integer> addEvaluate(OrderEvaluateParams params) {
        return mRepositoryManager.obtainRetrofitService(OrderService.class)
                .orderEvaluation(getRequestBody(JSON.toJSONString(params)))
                .compose(RxUtils.handleCode());
    }

    @Override
    public Observable<OrderDetailResult> getOrderById(int id) {
        return mRepositoryManager.obtainRetrofitService(OrderService.class)
                .getOrderById(id)
                .compose(RxUtils.handleResult());
    }

    @Override
    public Observable<ListResp<MyAddressResult>> fetchMyAddress(int page, int pageSize) {
        return mRepositoryManager.obtainRetrofitService(MineService.class)
                .fetchMyAddress(page, pageSize)
                .compose(RxUtils.handleResult());
    }

    @Override
    public Observable<OrderCreateResult> createOrder(OrderCreateParams params) {
        return mRepositoryManager.obtainRetrofitService(OrderService.class)
                .createServiceOrder(getRequestBody(JSON.toJSONString(params)))
                .compose(RxUtils.handleResult());
    }

    @Override
    public Observable<Map<String, Object>> payOrder(OrderPayParams payParams) {
        return mRepositoryManager.obtainRetrofitService(OrderService.class)
                .payOrder(getRequestBody(JSON.toJSONString(payParams)))
                .compose(RxUtils.handleResult());
    }

    @Override
    public Observable<Integer> cancelOrder(int orderId) {
        return mRepositoryManager.obtainRetrofitService(OrderService.class)
                .cancelOrder(orderId)
                .compose(RxUtils.handleCode());
    }

    @Override
    public Observable<Integer> deleteOrder(int orderId) {
        return mRepositoryManager.obtainRetrofitService(OrderService.class)
                .deleteOrder(orderId)
                .compose(RxUtils.handleCode());
    }

    @Override
    public Observable<Integer> refundOrder(OrderRefundParams params) {
        return mRepositoryManager.obtainRetrofitService(OrderService.class)
                .refundOrder(getRequestBody(GsonUtils.toJson(params)))
                .compose(RxUtils.handleCode());
    }

    @Override
    public Observable<Integer> cancelRefund(int orderId) {
        return mRepositoryManager.obtainRetrofitService(OrderService.class)
                .cancelRefund(orderId)
                .compose(RxUtils.handleCode());
    }

    @Override
    public Observable<String> getQINiuToken() {
        return mRepositoryManager.obtainRetrofitService(CommonService.class)
                .getQiNiuToken()
                .compose(RxUtils.handleResult());
    }

    @Override
    public Observable<HouseKeeperResult> getHouseKeeperResult() {
        return mRepositoryManager.obtainRetrofitService(MineService.class)
                .getMineHouseKeeper()
                .compose(RxUtils.handleResult());
    }
}
